﻿Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
''' <summary>
''' Das Grundobjekt fuer alle 2D Menue Objekte
''' </summary>
''' <remarks></remarks>
Public MustInherit Class Objekt2D
    Public Shared HasFocus As Boolean = False
    Public Shared Texturen As Textur() = {}
    Public Shared Objekte As Objekt2D() = {}
    Public Shared InputTextBox As TextBox
    Protected Handler As ObjektPanel
    Protected Fixed As PointF
    Public Shared Sub Reset()
        Dim I As Integer = 0
        While I < Objekte.Length
            Objekte(I).RemoveHandles()
        End While
    End Sub
    Dim Focuse As Boolean = False
    ReadOnly Property Focused() As Boolean
        Get
            Return Focuse
        End Get
    End Property
    Public Sub Focus()
        For I = 0 To Objekte.Length - 1
            Objekte(I).Focuse = False
            Objekte(I).RemoveFocus()
        Next
        HasFocus = True
        Me.Focuse = True
    End Sub
    ''' <summary>
    ''' Alle Objekte verlieren ihren Focus
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub RemoveAllFocus()
        For I = 0 To Objekte.Length - 1
            Objekte(I).Focuse = False
        Next
        HasFocus = False
    End Sub
    Public Overridable Sub RemoveFocus()
        HasFocus = False
        Me.Focuse = False
    End Sub
    Protected TexturID As Integer = -1
    Structure Textur
        Public Sub New(ByVal Texturname As String)
            T = Grafikengine.LoadTexture(DataSettings.Resourcenpfad + "Objects\" + Texturname + ".png")
            Me.Texturnname = Texturname
            GroesseGesamt = New Size(100, 100)
        End Sub
        Public Shared Function getTextur(ByVal ID As Integer) As Texture
            Return Texturen(ID).Textur
        End Function
        Private T As Texture
        Public ReadOnly Property Textur() As Texture
            Get
                Return T
            End Get
        End Property
        Private Texturnname As String
        Public ReadOnly Property Texturname() As String
            Get
                Return Texturnname
            End Get
        End Property
        Private GroessenGesamt As Size
        Public Property GroesseGesamt() As Size
            Get
                Return GroessenGesamt
            End Get
            Set(ByVal value As Size)
                GroessenGesamt = value
            End Set
        End Property
    End Structure
    Public Shared Sub RemoveObjekt(ByVal Objekt As Objekt2D)
        For I = 0 To Objekte.Length - 1
            If Objekte(I).Equals(Objekt) Then
                Objekte(I) = Objekte(Objekte.Length - 1)
                Array.Resize(Objekte, Objekte.Length - 1)
                Exit Sub
            End If
        Next
    End Sub

    Protected Bildgroesse As Rectangle
    Public Property Rechteck() As Rectangle
        Set(ByVal value As Rectangle)
            Bildgroesse = value
        End Set
        Get
            Return Bildgroesse
        End Get
    End Property

    Protected MaxeFrame As Point
    Public ReadOnly Property Maxframe() As Point
        Get
            Return MaxeFrame
        End Get
    End Property
    Private Frames As Point
    Public Property Frame() As Point
        Get
            Return Frames
        End Get
        Set(ByVal value As Point)
            Frames = value
            If Frames.X > MaxeFrame.X Then
                Frames.X = 0
            End If
            If Frames.Y > MaxeFrame.Y Then
                Frames.Y = 0
            End If
            Bildgroesse = New Rectangle(Bildgroesse.Width * Frames.X, Bildgroesse.Height * Frames.Y, Bildgroesse.Width, Bildgroesse.Height)
        End Set
    End Property
    Protected Koordinatene As Point
    Public Property Koordinaten() As Point
        Get
            Return Koordinatene
        End Get
        Set(ByVal value As Point)
            Koordinatene = value
        End Set
    End Property

    Private Zoome As Single
    Public Property Zoom() As Single
        Get
            Return Zoome
        End Get
        Set(ByVal value As Single)
            Zoome = value
        End Set
    End Property

    Public Sub New(ByVal Texturname As String, ByVal Bildgroesse As Rectangle, ByVal Position As Point, ByVal Fixed As PointF, Optional ByVal Handler As ObjektPanel = Nothing)
        If Texturname <> "" Then
            If Texturname.Contains("\") Then
                Texturname = Texturname.Remove(0, Texturname.LastIndexOf("\") + 1)
            End If
            If Texturname.EndsWith(".png") Then
                Texturname = Texturname.Remove(Texturname.LastIndexOf("."))
            End If
            TexturID = -1
            For I = 0 To Texturen.Length - 1
                If Texturen(I).Texturname = Texturname Then
                    TexturID = I
                    Exit For
                End If
            Next
            If TexturID < 0 Then
                Array.Resize(Texturen, Texturen.Length + 1)
                Texturen(Texturen.Length - 1) = New Textur(Texturname)
                TexturID = Texturen.Length - 1
            End If
        End If
        Me.Fixed = Fixed
        Koordinatene = Position
        Me.Bildgroesse = Bildgroesse
        Frames = New Point(0, 0)
        If TexturID >= 0 Then
            MaxeFrame = New Point(Math.DivRem(Texturen(TexturID).GroesseGesamt.Width, Bildgroesse.Width, 0), Math.DivRem(Texturen(TexturID).GroesseGesamt.Height, Bildgroesse.Height, 0))
        End If
        Array.Resize(Objekte, Objekte.Length + 1)
        Objekte(Objekte.Length - 1) = Me
        Me.Handler = Handler
        If Me.Handler IsNot Nothing Then
            Me.Handler.AddObjekt(Me)
        End If
    End Sub
    Public MustOverride Function Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
    Public MustOverride Function KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
    Public MustOverride Function MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As Boolean
    Public MustOverride Function MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As String
    Public MustOverride Function MouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As Boolean
    Public MustOverride Function MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As Boolean
    Public Function MouseDown(ByVal X As Integer, ByVal Y As Integer) As Boolean
        Return MouseDown(Nothing, New System.Windows.Forms.MouseEventArgs(MouseButtons.Left, 1, X, Y, 0))
    End Function
    Public Function MouseWheel(ByVal X As Integer, ByVal Y As Integer, ByVal delta As Integer) As Boolean
        Return MouseDown(Nothing, New System.Windows.Forms.MouseEventArgs(MouseButtons.None, 1, X, Y, delta))
    End Function
    Public Function MouseUp(ByVal X As Integer, ByVal Y As Integer) As String
        Return MouseUp(Nothing, New System.Windows.Forms.MouseEventArgs(MouseButtons.Left, 1, X, Y, 0))
    End Function
    Public Function MouseEnter(ByVal X As Integer, ByVal Y As Integer) As Boolean
        Return MouseEnter(Nothing, New System.Windows.Forms.MouseEventArgs(MouseButtons.None, 0, X, Y, 0))
    End Function
    Public Overridable Sub Malen()
        If Handler IsNot Nothing Then
            If Handler.IsVisible(New Rectangle(Koordinaten.X, Koordinaten.Y, Bildgroesse.Width * Zoome, Bildgroesse.Height * Zoome), True) Then
                Grafikengine.Sprite.Draw2D(Textur.getTextur(TexturID), Bildgroesse, New SizeF(Bildgroesse.Width * Zoome, Bildgroesse.Height * Zoome), New Point(), 0, GetRealKoordinaten, Color.FromArgb(255, 255, 255, 255))
            End If
        Else
            Grafikengine.Sprite.Draw2D(Textur.getTextur(TexturID), Bildgroesse, New SizeF(Bildgroesse.Width * Zoome, Bildgroesse.Height * Zoome), New Point(), 0, Koordinatene, Color.FromArgb(255, 255, 255, 255))
        End If
    End Sub
    ''' <summary>
    ''' Entfernt moegliche Handles
    ''' </summary>
    ''' <remarks></remarks>
    Public Overridable Sub RemoveHandles()
        RemoveObjekt(Me)
    End Sub
    ''' <summary>
    ''' Wenn die Fenster groesse geaendert wird kommen wir hier her.
    ''' </summary>
    ''' <param name="PosChange"></param>
    ''' <remarks></remarks>
    Public Overridable Sub ChangeWindowSize(ByVal PosChange As Point)
        Koordinatene.X += (PosChange.X * Fixed.X)
        Koordinatene.Y += (PosChange.Y * Fixed.Y)
    End Sub
    Public Function GetRealX() As Integer
        If Handler Is Nothing Then
            Return Koordinaten.X
        Else
            Return Koordinaten.X + Handler.InternePosition.X + Handler.Koordinaten.X
        End If
    End Function
    Public Function GetRealY() As Integer
        If Handler Is Nothing Then
            Return Koordinaten.Y
        Else
            Return Koordinaten.Y + Handler.InternePosition.Y + Handler.Koordinaten.Y
        End If
    End Function
    Public Function GetRealKoordinaten() As Point
        Return New Point(GetRealX, GetRealY)
    End Function
    Public Function GetHandlerPosition() As Point
        If Handler Is Nothing Then
            Return New Point()
        Else
            Return New Point(Handler.InternePosition.X + Handler.Koordinaten.X, Handler.InternePosition.Y + Handler.Koordinaten.Y)
        End If
    End Function
    Protected Overridable Function IsVisble(Optional ByVal PlusX As Integer = 0, Optional ByVal PlusY As Integer = 0) As Boolean
        If Handler IsNot Nothing Then
            If Handler.IsVisible(New Rectangle(Koordinaten.X, Koordinaten.Y, Rechteck.Width + PlusX, Rechteck.Height + PlusY), True) Then
                Return True
            Else
                Return False
            End If
        Else
            Return True
        End If
    End Function
End Class
